# coding=utf-8
# author:WHY
# time:2016-05-24

from myParser import Parser
import sys
import argparse

parser = argparse.ArgumentParser(
    description='This tool is a parser made by WHY')
parser.add_argument('-g', action="store", dest='gra', required=True,
    help='The grammar file path')
parser.add_argument('-l', action="store", dest='token', required=True,
    help='The lexical output file path')
args = parser.parse_args()
args = vars(args)           # converts the arguments into dictionary format for easier retrieval

token = []
fileInput = open(args['token'], 'r')
for line in fileInput.readlines():
	if line[-1] == '\n':
		line = line[:-1]
	if len(line) == 0:
		continue
	line = line.split('\t')
	line = line[0].split(':') + line[1:]
	token.append(line)
fileInput.close()

grammar = {}
fileInput = open(args['gra'], 'r')
for line in fileInput.readlines():
	if line[-1] == '\n':
		line = line[:-1]
	if len(line) == 0:
		continue
	arr = line.split(' ')
	if arr[0] not in grammar.keys():
		grammar.setdefault(arr[0], [])
	temp = arr[1:]
	if temp[-1] == '&':
		temp = temp[:-1]
	temp = tuple(temp)
	grammar[arr[0]].append(temp)
fileInput.close()

par = Parser(token, grammar, '!program')
print par.parser()

'''for item in par.c:
	print '\n\n'
	print 'ID:', item['id']
	print 'ACTION:'
	for key, value in par.action[item['id']].items():
		print '\t', key, value['data'], value['flag']
	print 'GOTO'
	if item['id'] in par.goto.keys():
		for key, value in par.goto[item['id']].items():
			print '\t', key, value
	print 'DATA:'
	for value in item['data']:
		print '\t', value['data'], value['flag'], value['pointPosition']
		
for key, item in par.hashSet.items():
	print 'ID:', key
	print '\t', item'''
	